package ecustom.finance.bean;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.ecustom.finance.WorkflowAction;

import ecustom.common.conn.DB;
import ecustom.common.util.CustomUtil;

/**
* @author 作者:Gcc
* @createDate 创建时间：Aug 21, 2023 10:32:37 AM
*/
public class WorkflowBean extends WorkflowAction{
	
	private static final Logger log = Logger.getLogger(WorkflowBean.class); 
	
	/**
	 * 处理凭证
	 * @throws SQLException 
	 */
	public JSONObject handlVoucher(List<Map<String, Object>> listVoucher) throws Exception {

		JSONObject checkVoucher =  chechListVoucherV2(listVoucher);
		if(200 != checkVoucher.getInteger("code")) return checkVoucher;
		
		JSONObject dataJson = new JSONObject();
		JSONArray array = new JSONArray();
		boolean bl = true;
		String message = "";
		for(Map<String, Object> voucher : listVoucher) {
			String requestid = CustomUtil.getString(voucher.get("requestid"));
			String voucherNumber = CustomUtil.getString(voucher.get("voucherNumber"));
			String voucherStr = CustomUtil.getString(voucher.get("voucherStr"));
			
			JSONObject voucherJson = updateVoucher(voucherNumber, voucherStr, requestid);
			Integer vouvcherCode = voucherJson.getInteger("code");
			if(vouvcherCode != 200) {
				bl = false;
				message += voucherJson.getString("message");
			}
			array.add(voucherJson);
		}
		if(bl) {
			dataJson.put("code", 200);
		}else {
			dataJson.put("code", 500);
		}
		dataJson.put("data", array);
		dataJson.put("message", message);
		return dataJson;
	}
	
	private JSONObject updateVoucher(String voucherNumber, String voucherStr, String requestid) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT T2.FORMID FROM WORKFLOW_REQUESTBASE T1 ");
		sb.append(" INNER JOIN WORKFLOW_BASE T2 ON T2.ID = T1.WORKFLOWID ");
		sb.append(" WHERE T1.REQUESTID = ? ");
		Integer formid = DB.conn().getInteger(sb.toString(), requestid);
		
		if(CustomUtil.isBlank(formid)) {
			return errReq(requestid, "【requestid="+requestid+"】修改凭证号失败！根据requestid无法获取OA对应流程单据，请联系管理员！");
		}
		
		String tableName = "FORMTABLE_MAIN_"+Math.abs(formid);
		String updateSql = "UPDATE "+tableName + " SET VOUCHERNUMBER=?, VOUCHERSTR=? WHERE REQUESTID=?";
		int code = DB.conn().executeUpdate(updateSql, voucherNumber, voucherStr, requestid);
		if(code < 0) {
			return errReq(requestid, "【requestid="+requestid+"】修改凭证号失败！请联系管理员！");
		}else {
			return okReq(requestid, "修改凭证成功！");
		}
	}
	
	private JSONObject chechListVoucher(List<Map<String, Object>> listVoucher) {
		JSONObject checkVoucher = new JSONObject();
		JSONArray array = new JSONArray();
		for(Map<String, Object> voucher : listVoucher) {
			String requestid = CustomUtil.getString(voucher.get("requestid"));
			String voucherNumber = CustomUtil.getString(voucher.get("voucherNumber"));
			String voucherStr = CustomUtil.getString(voucher.get("voucherStr"));
			String errorMsg = "";
			if(CustomUtil.isBlank(requestid)) errorMsg += "OA流程唯一单号不能为空！";
			if(CustomUtil.isBlank(voucherNumber)) errorMsg += "凭证号不能为空！"; 
			if(CustomUtil.isBlank(voucherStr)) errorMsg += "展示凭证号不能为空！";
			
			if(CustomUtil.isNotBlank(errorMsg)) {
				array.add(errReq("", errorMsg));
			}
		}
		if(array.size() > 0) {
			checkVoucher.put("code", 500);
		}else {
			checkVoucher.put("code", 200);
		}
		checkVoucher.put("data", array);
		return checkVoucher;
	}
	
	private JSONObject chechListVoucherV2(List<Map<String, Object>> listVoucher) {
		JSONObject checkVoucher = new JSONObject(); 
		JSONArray errorArray = new JSONArray();
		String message = "";
		for(Map<String, Object> voucher : listVoucher) {
			String requestid = CustomUtil.getString(voucher.get("requestid"));
			String voucherNumber = CustomUtil.getString(voucher.get("voucherNumber"));
			String voucherStr = CustomUtil.getString(voucher.get("voucherStr"));
			String errorMsg = "";
			if(CustomUtil.isBlank(requestid)) errorMsg += "OA流程唯一单号不能为空！";
			if(CustomUtil.isBlank(voucherNumber)) errorMsg += "【requestid="+requestid+"】凭证号不能为空！"; 
			if(CustomUtil.isBlank(voucherStr)) errorMsg += "【requestid="+requestid+"】展示凭证号不能为空！";
			
			if(CustomUtil.isNotBlank(errorMsg)) {
				errorArray.add(errReq(requestid+"", errorMsg));
				message += errorMsg;
			}
		}
		if(errorArray.size() > 0) {
			checkVoucher.put("code", 500);
			checkVoucher.put("data", errorArray);
			checkVoucher.put("message", message);
			return checkVoucher;
		}else {
			checkVoucher.put("code", 200);
			checkVoucher.put("data", "");
			checkVoucher.put("message", "");
			return checkVoucher;
		}
	}
	
	
	
	
	
}
